<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
//执行任务
model('jy_weishop/task')->orderup();
model('jy_weishop/task')->int_comments();
//未登录下单
model('jy_weishop/member')->checklogin();
$return['show_setmobile']=cfc('core')->check_setmobile(2);
$return['show_setmember']=cfc('core')->check_setmember(2);
//检查异常订阅
if($_SESSION['subscribeid']){
	if(function_exists('uperror_publish')){
		uperror_publish($_SESSION['subscribeid']);
	}
}
if($_RQ['op']=='main'){
	$shopset=plugin('jy_weishop')->plugin_setting('shopset');
	if($_RQ['gopayaddpriceid']){
		$return['gopayaddpriceid']=intval($_RQ['gopayaddpriceid']);
	}
	//返回链接
	$return['forward_url']=base64_encode('jy_weishop/order.list//fromcreate=1');
	$isfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');	
	if(!$isfollow && $_RQ['fromcreate'] && !$shopset['noshowqrcode']){
		$return['showwechatcode']=1;
	}
	$return['fromcreate']=intval($_RQ['fromcreate']);
	if($return['fromcreate']){
		$neworder=pdo_get('jy_weishop_order',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'','id desc');	
		if($neworder['merchid'] && $this->plugin_exists('jy_merch')){
			$return['redfee']=pdo_getcolumn('jy_merch_feelog',array('createtime >'=>TIMESTAMP-10,'type'=>0,'merchid'=>$neworder['merchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'fee');	
			if($return['redfee']>0){
				$jy_merch=plugin('jy_merch')->plugin_setting('setting');
				$return['fanred_mp3']=$jy_merch['fanred_mp3'];
			}
		}
	}
	$return['list']=array();
	$return['status']=isset($_RQ['status'])?$_RQ['status']:100;
	$return['params']['status']=$return['status'];
	//先关闭 下单弹窗就行了！获取小程序订阅消息ID
	if($_SESSION['wxapp_id'] && false){
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('6271',[1,13,2,5,8],'pay2buyer','订单支付成功通知','jy_weishop');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
		if($citypeisong['isopen']){
			$tmpid=cfc('wxapp_api')->get_tpl_template_id('17468',[2,3],'pei2buyer','订单配送提醒','jy_weishop');
		}else{
			$tmpid=cfc('wxapp_api')->get_tpl_template_id('3637',[1,2,4,5,9],'send2buyer','订单发货通知','jy_weishop');
		}
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('12340',[1,2,3,4],'receive2buyer','订单确认收货通知','jy_weishop');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$return['tmplIds']=$tmplIds;
	}
	$return['feemoney']=plugin('jy_weishop')->plugin_setting('feemoney');
	if($this->plugin_exists('jy_merch')){
		$return['merchred']=model('jy_merch/model')->get_nowred();
	}
	if($return['merchred']>0){
		$return['merchredmp3']=plugin('jy_merch')->plugin_setting('setting')['red_mps'];
	}
	if(!$return['merchred']){
		$return['coupon_wait']=model('jy_weishop/task')->pop_coupon_wait();
		$return['coupon_wait']=$return['coupon_wait']?$return['coupon_wait'][0]:false;
	}
	if(!$return['merchred'] && !$return['coupon_wait']){
			$return['coupon_hd']=model('jy_weishop/task')->pop_coupon_hd();
			if(!$return['coupon_hd']){
				$return['game_wait']=model('jy_weishop/task')->pop_game();
			}
	}
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('mobile','avatar','nickname'));
	if($return['pc_set']['is_en']==1){
		set_html('title','Order list');
	}else{
		set_html('title','订单列表');
	}
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'userdeleted'=>0);
	$list=model('jy_weishop/get_order')->getlist($params,$_RQ);
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='pay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($order['ding_price']>0){
		$dingorder=pdo_get('jy_weishop_order_ding',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
		if($dingorder['status']!=2){
			exi('该订单的保证金还没交或还未成单，不能支付余款！','error');
		}
	}
	exi($order['pay_id']);
}
if($_RQ['op']=='cancel'){
	$post=get_postdata();	
	$id=intval($post['id']);
	if($id){
		model('jy_weishop/order')->order_cancel($id);
		exi('取消成功！','','jy_weishop/order.list/');		
	}else{		
		exi('取消失败！','error');
	}

}
if($_RQ['op']=='receive'){
	$post=get_postdata();
	$id=intval($post['id']);
	model('jy_weishop/order')->order_receive($id);
	exi('收货成功！');	
}
//支付订单
if($_RQ['op']=='payaddfee'){
	$id=intval($_RQ['id']);
	$order=pdo_get('jy_weishop_order',array('id'=>$id));
	$isorder=pdo_get('jy_weishop_order_addfee',array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$order['mid'],
			'orderid'=>intval($order['id'])
			)
	);
	if($isorder['id']){
		$payid=$isorder['pay_id'];
		$order_id=$isorder['id'];
	}else{
		$order_id=model('jy_weishop/order')->createaddfee_order($order);
		$params=array(
			'id'=>$order_id,
			'title'=>'购买商品',
			'fee'=>$order['addprice'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_mcaigou/order.list/addpayresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$success_url?:'jy_mcaigou/order.list/',
			'error_url'=>$error_url?:'jy_mcaigou/order.list/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_weishop_order_addfee',array('pay_id'=>$payid),array('id'=>$order_id));
	}
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='addpayresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_mcaigou/order.list/');
	}
	$order=pdo_get('jy_weishop_order_addfee',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_mcaigou/order.list/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_mcaigou/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	model('jy_weishop/order')->order_addfeepay($order['id'],$params);
	exi('提交成功!');
}
if($_RQ['op']=='buyto'){
	$orderid=intval($_RQ['orderid']);
	$order=pdo_get('jy_weishop_order',array('id'=>$orderid));
	$storeid=$order['storeid'];
	$merchid=$order['merchid'];
	$sendtype=$order['sendtype'];
	$is_dish=$order['is_dish'];
	$cart_list=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'orderid'=>$orderid),array('id','goodsid','total','optionid','merchid'));
	if(!$cart_list){
		exi('该订单没有商品！','error');
	}
	foreach($cart_list as &$cart){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$cart['goodsid']),array('id'));
			if(!$goods){
				exi('指定商品不存在！','error');
			}
			//$this->model('goods')->check_buy($goods['id'],$cart['total'],$cart['optionid']);
	}
	$_SESSION['cart_list']=$cart_list;
	if($_SESSION['current_plugin']=='jy_weidish' || $_SESSION['current_plugin']=='jy_weishoppei' || $_SESSION['current_plugin']=='jy_weishoppeishop' || $_SESSION['current_plugin']=='jy_weishopscale'){
		if($_SESSION['current_plugin']=='jy_weishoppeishop'){
			exi('','','jy_weishoppei/create//isbuyto=1');
		}elseif($_SESSION['current_plugin']=='jy_weishopscale'){
			exi('','','jy_weishopscale/create//isbuyto=1');
		}else{
			exi('','',$_SESSION['current_plugin'].'/create//isbuyto=1');
		}
	}else{
		exi('','','jy_mcaigou/order.create//isbuyto=1&storeid='.$storeid.'&merchid='.$merchid.'&sendtype='.$sendtype.'&is_dish='.$is_dish);
	}
}
?>